/**
     * 
     */

package com.xyz.frame.admin.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xyz.frame.bean.model.User;
import com.xyz.frame.common.util.PageParam;
import com.xyz.frame.security.common.bo.UserInfoInTokenBO;
import com.xyz.frame.security.common.manager.TokenStore;
import com.xyz.frame.security.common.util.AuthUserContext;
import com.xyz.frame.service.UserService;
import com.xyz.frame.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import com.xyz.frame.common.response.ServerResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;


/**
 *  on 2018/10/16.
 */
@RestController
@RequestMapping("/admin/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private TokenStore tokenStore;


    /**
     * 分页获取
     */
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('admin:user:page')")
    public ServerResponseEntity<IPage<User>> page(User user,PageParam<User> page) {
        IPage<User> userPage = userService.page(page, new LambdaQueryWrapper<User>()
                .like(StrUtil.isNotBlank(user.getUserName()), User::getUserName, user.getUserName())
                .eq(user.getStatus() != null, User::getStatus, user.getStatus()));
        for (User userResult : userPage.getRecords()) {
            userResult.setUserName(StrUtil.isBlank(userResult.getUserName()) ? "" : userResult.getUserName());
        }
        return ServerResponseEntity.success(userPage);
    }

    /**
     * 获取信息
     */
    @GetMapping("/info/{userId}")
    @PreAuthorize("@pms.hasPermission('admin:user:info')")
    public ServerResponseEntity<User> info(@PathVariable("userId") String userId) {
        User user = userService.getById(userId);
        user.setUserName(StrUtil.isBlank(user.getUserName()) ? "" : user.getUserName());
        return ServerResponseEntity.success(user);
    }

    /**
     * 修改
     */
    @PutMapping
    @PreAuthorize("@pms.hasPermission('admin:user:update')")
    public ServerResponseEntity<Void> update(@RequestBody User user) {
        user.setModifyTime(new Date());
        user.setUserName(StrUtil.isBlank(user.getUserName()) ? "" : user.getUserName());
        userService.updateById(user);
        return ServerResponseEntity.success();
    }

    /**
     * 删除
     */
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('admin:user:delete')")
    public ServerResponseEntity<Void> delete(@RequestBody String[] userIds) {
        userService.removeByIds(Arrays.asList(userIds));
        return ServerResponseEntity.success();
    }

    /**
     * 根据token获取用户信息
     */
    @GetMapping("/info")
    public ServerResponseEntity<Map<String, Object>> info(){
        Map<String, Object> userInfo = sysUserService.getUserInfo();
        return ServerResponseEntity.success(userInfo);
    }

}
